//
// See the file COPYING for redistribution information.
//

#include <iostream>
#include <Naipax.hpp>

using namespace std;
using namespace Naipax;

void Test(const char *pUrl) {
    if (pUrl == NULL) {
        NAIPAX_THROW(Error::INVALID_ARGUMENT);
    }

    Url UrlStr(pUrl);
    cout << "--------------------------------------" << endl;
    cout << "Is valid? " << UrlStr.IsValid() << endl;
    cout << "Scheme: " << UrlStr.Scheme() << endl;
    cout << "Authority: " << UrlStr.Authority() << endl;
    cout << "Host: " << UrlStr.Host() << endl;
    cout << "Domain Levels: " << UrlStr.DomainLevels() << endl;
    for (uint32_t i = 1; i <= UrlStr.DomainLevels() + 1; i++) {
        cout << "SubHostname[" << i << "]: " << UrlStr.SubHostname(i) << endl;
    }
    cout << "Port: " << UrlStr.Port() << endl;
    cout << "Path: " << UrlStr.Path() << endl;
    cout << "Query: " << UrlStr.Query() << endl;
        
    cout << "Before normalization: " << UrlStr.AsString() << endl;

    UrlStr.Normalize();

    cout << "Is valid? " << UrlStr.IsValid() << endl;
    cout << "Scheme: " << UrlStr.Scheme() << endl;
    cout << "Authority: " << UrlStr.Authority() << endl;
    cout << "Host: " << UrlStr.Host() << endl;
    cout << "Domain Levels: " << UrlStr.DomainLevels() << endl;
    for (uint32_t i = 1; i <= UrlStr.DomainLevels() + 1; i++) {
        cout << "SubHostname[" << i << "]: " << UrlStr.SubHostname(i) << endl;
    }
    cout << "Port: " << UrlStr.Port() << endl;
    cout << "Path: " << UrlStr.Path() << endl;
    cout << "Query: " << UrlStr.Query() << endl;

    cout << "After normalization: " << UrlStr.AsString() << endl;
}

int main() {
    cerr << "Test for the Url module" << endl;

    try {
        char UrlList[10][256] = {
            "HttP://user:password@Somesite.COM:80/./.././../root/./path/./a/../b/c/.././././index.html?key1=value1&key2=value2#top",
            "http://tantek.com/log/2007/02.html#d19t1813",
            "http://www.nytimes.com/2007/02/18/movies/18halb.html?ex=1329454800&amp;en=41d4248507170297&amp;ei=5090&amp;partner=rssuserland&amp;emc=rss&amp;pagewanted=all"
            "\0",
        };
        for (uint32_t i = 0; i < 10; ++i) {
            if (UrlList[i][0] == '\0') {
                break;
            }
            Test(UrlList[i]);
        }
    }
    catch (Naipax::Error &E) {
        NAIPAX_PERROR(E.What());
        return -1;
    }
    return 0;
}
